package com.example.calendar.ui.screens.settings

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

/**
 * ViewModel for the Settings screen
 */
@HiltViewModel
class SettingsViewModel @Inject constructor(
    application: Application
) : AndroidViewModel(application) {

    private val dataStore = application.dataStore
    
    // Preference keys
    private companion object {
        val FIRST_DAY_OF_WEEK = intPreferencesKey("first_day_of_week")
        val NOTIFICATIONS_ENABLED = booleanPreferencesKey("notifications_enabled")
        val SYNC_ENABLED = booleanPreferencesKey("sync_enabled")
    }
    
    // App version
    val appVersion: String = try {
        val packageInfo = application.packageManager.getPackageInfo(application.packageName, 0)
        "${packageInfo.versionName} (${packageInfo.versionCode})"
    } catch (e: Exception) {
        "1.0.0"
    }
    
    // First day of week preference (0 = Sunday, 1 = Monday, etc.)
    val firstDayOfWeek: Flow<Int> = dataStore.data.map { preferences ->
        preferences[FIRST_DAY_OF_WEEK] ?: 0 // Default to Sunday
    }
    
    // Notifications enabled preference
    val notificationsEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[NOTIFICATIONS_ENABLED] ?: true // Default to enabled
    }
    
    // Sync enabled preference
    val syncEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[SYNC_ENABLED] ?: false // Default to disabled
    }
    
    // Day of week picker state
    private val _showDayOfWeekPickerDialog = MutableStateFlow(false)
    val showDayOfWeekPickerDialog: Boolean
        get() = _showDayOfWeekPickerDialog.value
    
    /**
     * Update the first day of week preference
     */
    suspend fun setFirstDayOfWeek(day: Int) {
        dataStore.edit { preferences ->
            preferences[FIRST_DAY_OF_WEEK] = day
        }
    }
    
    /**
     * Update the notifications enabled preference
     */
    suspend fun setNotificationsEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[NOTIFICATIONS_ENABLED] = enabled
        }
    }
    
    /**
     * Update the sync enabled preference
     */
    suspend fun setSyncEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[SYNC_ENABLED] = enabled
        }
    }
    
    /**
     * Show the day of week picker dialog
     */
    fun showDayOfWeekPicker() {
        _showDayOfWeekPickerDialog.value = true
    }
    
    /**
     * Hide the day of week picker dialog
     */
    fun hideDayOfWeekPicker() {
        _showDayOfWeekPickerDialog.value = false
    }
} 